export default function updateNodeElement (dom, virtualDom, oldVirtual = {}) {
  const newProps = virtualDom.props || {}
  const oldProps = oldVirtual.props || {}


  Object.keys(newProps).forEach(attr => {

    if (newProps[attr] !== oldProps[attr]) {
      if (attr.startsWith('on')) {
        const eventType = attr.substr(2).toLowerCase()
        dom.addEventListener(eventType, newProps[attr], false)

        if (oldProps[attr]) {
          dom.removeEventListener(eventType, oldProps[attr])
        }

      } else if (attr === 'value' || attr === 'check') {
        dom[attr] = newProps[attr]
      } else if (attr === 'className') {
        dom.setAttribute('class', newProps[attr])
      } else if (attr !== 'children') {
        dom.setAttribute(attr, newProps[attr])
      }
    }
    
  })

  

  Object.keys(oldProps).forEach(attr => {
    if (newProps[attr] !== oldProps[attr]) {
      if (attr.startsWith('on')) {
        const eventType = attr.substr(2).toLowerCase()
        
        dom.removeEventListener(eventType, oldProps[attr])
      } else if (attr !== 'children') {
        dom.removeAttribute(attr)
      }
    }
  })
}